package net.bbtang.website.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.bbtang.website.mapper.RandomWordMapper;
import net.bbtang.website.mapper.SignInfoMapper;
import net.bbtang.website.pojo.RandomWordDTO;
import net.bbtang.website.pojo.ScoreInfoDTO;
import net.bbtang.website.pojo.ScoreInfoRefDTO;
import net.bbtang.website.pojo.UserSignInfoDTO;
import net.bbtang.website.pojo.UserSignInfoDetailDTO;
import net.bbtang.website.service.ScoreInfoService;
import net.bbtang.website.service.SignInfoService;
import net.bbtang.website.service.UserStatisticsService;
import net.bbtang.website.utils.ConstantsStatus;
import net.bbtang.website.utils.RandomWordStatus;
import net.bbtang.website.utils.ScoreConstantsStatus;
import net.bbtang.website.utils.StatusCode;




@Service("signInfoService")
public class SignInfoServiceImpl implements SignInfoService {
	
	private static final Logger logger = Logger.getLogger(SignInfoServiceImpl.class);
	
	@Autowired
	SignInfoMapper signInfoMapper;
	
	@Autowired
	RandomWordMapper randomWordMapper;
	
	@Autowired
	ScoreInfoService scoreInfoService;
	@Autowired
	UserStatisticsService userStatisticsService;
	
	
	@Override
	@Transactional
	public StatusCode<Object> addSignInfo(UserSignInfoDetailDTO userSignInfo) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		//检查传入日期是否已经签到
		List<UserSignInfoDetailDTO> userSignInfoList =  this.checkSignInfo(userSignInfo);
		RandomWordDTO randomWordDTO = new RandomWordDTO();
		randomWordDTO.setRandomFunction(RandomWordStatus.RANDOM_WORD_FUNCTION_SIGN);
		randomWordDTO.setRandomWordType(RandomWordStatus.RANDOM_WORD_ALL);
		RandomWordDTO randomWordResult = randomWordMapper.getRandomWord(randomWordDTO);
//		sc.setData(randomWordResult.getRandomWord());
		if(userSignInfoList != null && userSignInfoList.size() > 0){
			//已经签到
			sc.setFlag(true);
			sc.codes(ConstantsStatus.SC2002);
			sc.setData(randomWordResult);
			sc.setDetailMsg("今天已经签到");
			
		}else{
			//签到成功
			signInfoMapper.addSignInfo(userSignInfo);
			UserSignInfoDTO userStatistics = userStatisticsService.getStatistics(userSignInfo.getUserId());
			userStatistics.setScore(ScoreConstantsStatus.SIGN_IN);
			if(randomWordResult.getRandomWord() != null){
				userStatistics.setRandomWord(randomWordResult.getRandomWord());
			}
			sc.setFlag(true);
			sc.setData(userStatistics);
			sc.codes(ConstantsStatus.SC2000);
			sc.setDetailMsg("签到成功");
			Map<String,Object> map = new HashMap<String, Object>();
			//积分信息
			ScoreInfoRefDTO scoreInfoDetail = new ScoreInfoRefDTO();
			scoreInfoDetail.setChangeScore(ScoreConstantsStatus.SIGN_IN);
			scoreInfoDetail.setCreateTime(new Date());
			scoreInfoDetail.setUpdateTime(new Date());
			scoreInfoDetail.setScoreState(ScoreConstantsStatus.SCORE_PLUS);
			scoreInfoDetail.setScoreType(ScoreConstantsStatus.SCORE_SIGN);
			
			ScoreInfoDTO scoreInfo = new ScoreInfoDTO();
			scoreInfo.setUserId(userSignInfo.getUserId());
			scoreInfo.setUseState(ScoreConstantsStatus.SCORE_USE_STATE_ENABLE);
			scoreInfo.setUpdateTime(new Date());
			
			map.put("userId", userSignInfo.getUserId());
			map.put("scoreInfo", scoreInfo);
			map.put("scoreInfoDetail", scoreInfoDetail);
			//签到成功获取积分
			scoreInfoService.changeScoreInfo(map);
		}
	
		
		return sc;
	}
	
	@Override
	public List<UserSignInfoDetailDTO> checkSignInfo(UserSignInfoDetailDTO userSignInfo) {
		return signInfoMapper.checkSignInfo(userSignInfo);
	}

	@Override
	public List<String> getSignDateByUserId(Map<String, Object> map) {
		List<UserSignInfoDetailDTO> userSignInfoDTOList = signInfoMapper.getSignDateByUserId(map);
		List<String> signDateResult = new ArrayList<String>();
		if(userSignInfoDTOList != null && userSignInfoDTOList.size() > 0){
			for (UserSignInfoDetailDTO userSignInfoDTO : userSignInfoDTOList) {
				signDateResult.add(userSignInfoDTO.getSignDate());
			}
		}
		return signDateResult;
	}
	

}
